//
// This file is part of the aMule Project.
//
// Copyright (c) 2004-2006 Angel Vidal (Kry) ( kry@amule.org )
// Copyright (c) 2004-2006 aMule Team ( admin@amule.org / http://www.amule.org )
// Copyright (c) 2003 Barry Dunne (http://www.emule-project.net)
//
// Any parts of this program derived from the xMule, lMule or eMule project,
// or contributed by third-party developers are copyrighted by their
// respective authors.
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
// 
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301, USA
//

// Note To Mods //
/*
Please do not change anything here and release it..
There is going to be a new forum created just for the Kademlia side of the client..
If you feel there is an error or a way to improve something, please
post it in the forum first and let us look at it.. If it is a real improvement,
it will be added to the offical client.. Changing something without knowing
what all it does can cause great harm to the network if released in mass form..
Any mod that changes anything within the Kademlia side will not be allowed to advertise
there client on the eMule forum..
*/

#ifndef __KAD_UDP_LISTENER_H__
#define __KAD_UDP_LISTENER_H__

#include "UInt128.h"
#include "ByteIO.h"
#include "Types.h"
#include "Packet.h"
#include "Kademlia.h"
#include <iostream>
extern "C" {
#include "KadClog.h"
#include "net.h"
}

#define FIFOTIMEOUT (1000)	/* 15 seconds timeout on queue input */

////////////////////////////////////////
namespace std {
////////////////////////////////////////
class CMemFile;
//class CKademlia;
//class CSearch;

class CKademliaUDPListener
{
	friend class CSearch;

public:
	virtual ~CKademliaUDPListener() {};
	//CKademliaUDPListener(){out_interval = 60;};
	CKademliaUDPListener(CKademlia* instance)
	{
	  parentInstance = instance;
	  //out_interval = 60;
	};
	void bootstrap(uint32 ip, uint16 port);
	void firewalledCheck(uint32 ip, uint16 port);
	void sendMyDetails(byte opcode, uint32 ip, uint16 port);
	void sendNullPacket(byte opcode, uint32 ip, uint16 port);
	virtual void processPacket(const byte* data, uint32 lenData, uint32 ip, uint16 port);
	void sendPacket(const byte* data, uint32 lenData, uint32 destinationHost, uint16 destinationPort);
	void sendPacket(const byte *data, uint32 lenData, byte opcode, uint32 destinationHost, uint16 destinationPort);
	void sendPacket(unsigned char *data, uint32 lenData, byte opcode, uint32 destHost, uint16 destport);
	void sendPacket(CMemFile* data, byte opcode, uint32 destinationHost, uint16 destinationPort);
	bool getPacket(UDPIO *myUDP);
	unsigned long int PackPacket(unsigned char *packet, int size);
	bool openIPFile(void);
	void addHelloCount(uint32 IP);
  void sendSearchNotesReq(const CUInt128 &target, uint32 ip, uint16 port);
  void sendPublishNotesReq(
    const CUInt128 &target, SimpleTagList &tagList, uint32 ip, uint16 port);
	
	CKademlia *parentInstance;

	//Variables that used to be globals
	time_t lastoutput;
	unsigned long int out_interval;// = 60;
	unsigned long int bytes_recv;
	int packets_recv;
	
	
private:
	void addContact (const byte* data, uint32 lenData, uint32 ip, uint16 port, uint16 tport = 0);
	void addContacts(const byte* data, uint32 lenData, uint16 numContacts);
	//static SSearchTerm* CreateSearchExpressionTree(CMemFile& bio, int iLevel);
	//static void Free(SSearchTerm* pSearchTerms);


	void processBootstrapRequest		(const byte* packetData, uint32 lenPacket, uint32 ip, uint16 port);
	void processBootstrapResponse		(const byte* packetData, uint32 lenPacket, uint32 ip, uint16 port);
	void processHelloRequest		(const byte* packetData, uint32 lenPacket, uint32 ip, uint16 port);
	void processHelloResponse		(const byte* packetData, uint32 lenPacket, uint32 ip, uint16 port);
	void processKademliaRequest		(const byte* packetData, uint32 lenPacket, uint32 ip, uint16 port);
	void processKademliaResponse		(const byte* packetData, uint32 lenPacket, uint32 ip, uint16 port);
	void processSearchRequest		(const byte* packetData, uint32 lenPacket, uint32 ip, uint16 port);
	void processSearchResponse		(const byte* packetData, uint32 lenPacket, uint32 ip, uint16 port);
	void processPublishRequest		(const byte* packetData, uint32 lenPacket, uint32 ip, uint16 port);
	void processPublishResponse		(const byte* packetData, uint32 lenPacket, uint32 ip, uint16 port);
	void processSearchNotesRequest		(const byte* packetData, uint32 lenPacket, uint32 ip, uint16 port);
	void processSearchNotesResponse		(const byte* packetData, uint32 lenPacket, uint32 ip, uint16 port);
	void processPublishNotesRequest		(const byte* packetData, uint32 lenPacket, uint32 ip, uint16 port);
	void processPublishNotesResponse	(const byte* packetData, uint32 lenPacket, uint32 ip, uint16 port);
	void processFirewalledRequest		(const byte* packetData, uint32 lenPacket, uint32 ip, uint16 port);
	void processFirewalledResponse		(const byte* packetData, uint32 lenPacket, uint32 ip, uint16 port);
	void processFirewalledResponse2		(const byte* packetData, uint32 lenPacket, uint32 ip, uint16 port);
	void processFindBuddyRequest		(const byte* packetData, uint32 lenPacket, uint32 ip, uint16 port);
	void processFindBuddyResponse		(const byte* packetData, uint32 lenPacket, uint32 ip, uint16 port);
	void processCallbackRequest		(const byte* packetData, uint32 lenPacket, uint32 ip, uint16 port);



	unsigned char *putulong(unsigned char **ppb, unsigned long int u);
	unsigned char *putushort(unsigned char **ppb, unsigned short int u);
	unsigned long int getulong(unsigned char **ppb);
	unsigned short int getushort(unsigned char **ppb);
	unsigned char *putint128(unsigned char **ppb, const CUInt128& hash);
	FILE 		*IPlogfile;
	//void DebugClientOutput(const wxString& place, uint32 ip, uint32 port);
};

} // End namespace

#endif //__KAD_UDP_LISTENER_H__
